/******************************************************************************
 * Epigraph compiler                                                          *
 * Copyright © 2011 Grigorev Svyatoslav                                       *
 *                                                                            *
 * This program is free software: you can redistribute it and/or modify       *
 *     it under the terms of the GNU General Public License as published by   *
 *     the Free Software Foundation, either version 3 of the License, or      *
 *     (at your option) any later version.                                    *
 *                                                                            *
 *     This program is distributed in the hope that it will be useful,        *
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *     GNU General Public License for more details.                           *
 *                                                                            *
 *     You should have received a copy of the GNU General Public License      *
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 ******************************************************************************/

package epg;

import java.io.File;
import java.util.Stack;

/**
 * @author: Grigorev Svyatoslav
 * Date: epg 2011-08-27 10:56
 */
public class GenJava {
	static String javaFilePath;
	static WriteToFile writer = null;
	static String javaFileName;

	/**
	 * Определяем будующее имя класса
	 */

	static void init() {
		File f = new File(Location.Path);
		javaFilePath = f.getName();

		for (int i = 0; i < javaFilePath.length(); i++) {
			if (javaFilePath.charAt(i) == '.') {
				javaFilePath = javaFilePath.substring(0, i);
				break;
			}
		}
		javaFileName = new String(javaFilePath);
		javaFilePath += ".java";
		writer = new WriteToFile(javaFilePath);
	}

	static void addCode(String str) {
		writer.println(str);
	}

	static void genConst(Obj obj)
	{
		String str = new String("public final static ");
		if (obj.Typ == Table.typInt) {
			str += "int " + obj.Name + " = " + obj.Val;
		}
		addCode(str + ";");
	}

	static void genVar(Stack<Obj> stack, int type) {
		if (type == Table.typInt) {
			String str = new String("static int ");
			Obj obj = stack.pop();
			str += obj.Name;
			obj.Typ = Table.typInt;
			while (!stack.empty()) {
				obj = stack.pop();
				obj.Typ = Table.typInt;
				str += "," + obj.Name;
			}
			GenJava.addCode(str + ";");
		}

	}
}
